package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class BusinessRepositoryImpl implements BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public BusinessPO getById(Integer businessId) {
        log.debug("开始查询商家详情，入参：{}", businessId);
        return businessMapper.selectById(businessId);
    }

    @Override
    public void save(BusinessPO businessPO) {
        log.debug("保存商家信息，入参：{}", businessPO);
        businessMapper.insert(businessPO);
        log.debug("保存商家信息成功，返回：{}", businessPO.getId());
    }

    @Override
    public PageInfo<BusinessPO> pagesList(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = assembleBusinessQuery(query);
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }



    private QueryWrapper<BusinessPO> assembleBusinessQuery(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        //1.name 商家名称 模糊查询 非空 where name like(%#{}%)
        if (StringUtils.isNotBlank(query.getBusinessName())){
            queryWrapper.like("business_name",query.getBusinessName());
        }
        //2.负责人电话 模糊查询 非空 where phone like(%#{}%)
        if (StringUtils.isNotBlank(query.getBusinessHeadName())){
            queryWrapper.like("business_head_phone",query.getBusinessHeadName());
        }
        //3.负责人名称 模糊查询 非空 where head_name like(%#{}%)
        if (StringUtils.isNotBlank(query.getBusinessHeadName())){
            queryWrapper.like("business_head_name",query.getBusinessHeadName());
        }
        //4.商家状态 非空 where status = #{}
        if (query.getBusinessStatus() != null){
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        //5.开始时间 非空 where create_time >= #{}
        if (query.getStartingTime() != null){
            queryWrapper.ge("create_time",query.getStartingTime());
        }
        //6.结束时间 非空 where create_time <= #{}
        if (query.getEndTime() != null){
            queryWrapper.le("create_time",query.getEndTime());
        }
        return queryWrapper;
    }
    //7.商家名称 唯一

//8.公司地址 唯一
    @Override
    public boolean existsByCompanyAddress(String companyAddress) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_address", companyAddress);
        return businessMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public void updateBusiness(BusinessPO po) {
        businessMapper.updateById(po);
    }

    @Override
    public BusinessPO getBusinessById(Integer id) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return businessMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean existsByBusinessName(String businessName) {
        log.debug("检查商家名称是否存在：{}", businessName);
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_name", businessName);
        return businessMapper.selectCount(queryWrapper) > 0;
    }

}